// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "create_time": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "创建时间/注册时间",
    "defaultValue": {
      "$env": "now"
    },
    "label": "创建时间/注册时间"
  },
  "modify_time": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "最后更新时间",
    "defaultValue": {
      "$env": "now"
    },
    "label": "最后更新时间"
  },
  "admin_message": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "管理员备注",
    "label": "管理员备注"
  },
  "confirm_receiving_time": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "确认收货时间",
    "label": "确认收货时间"
  },
  "consignee": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "收件人",
    "label": "收件人"
  },
  "consignee_address": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "收件地址",
    "label": "收件地址"
  },
  "id_address": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "收件地址id",
    "label": "收件地址id"
  },
  "id_express": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "快递公司id",
    "label": "快递公司id"
  },
  "id_user": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "用户id",
    "label": "用户id"
  },
  "message": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "订单备注",
    "label": "订单备注"
  },
  "mobile": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "收件人电话",
    "label": "收件人电话"
  },
  "order_sn": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "订单号",
    "label": "订单号"
  },
  "pay_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "支付流水号",
    "label": "支付流水号"
  },
  "pay_status": {
    "rules": [
      {
        "format": "string"
      },
      {
        "range": [
          {
            "text": "未付款",
            "value": "1"
          },
          {
            "text": "已付款",
            "value": "2"
          },
          {
            "text": "支付中",
            "value": "3"
          }
        ]
      }
    ],
    "title": "支付状态",
    "label": "支付状态"
  },
  "pay_time": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "支付成功时间",
    "label": "支付成功时间"
  },
  "pay_type": {
    "rules": [
      {
        "format": "string"
      },
      {
        "range": [
          {
            "text": "支付宝",
            "value": "alipay"
          },
          {
            "text": "微信付款",
            "value": "wechat"
          }
        ]
      }
    ],
    "title": "实付类型",
    "label": "实付类型"
  },
  "real_price": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "实付金额",
    "label": "实付金额"
  },
  "shipping_amount": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "配送费用",
    "label": "配送费用"
  },
  "shipping_sn": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "快递单号",
    "label": "快递单号"
  },
  "shipping_time": {
    "rules": [
      {
        "format": "timestamp"
      }
    ],
    "title": "出库时间",
    "label": "出库时间"
  },
  "status": {
    "rules": [
      {
        "format": "int"
      }
    ],
    "title": "状态",
    "label": "状态"
  },
  "total_price": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "总金额",
    "label": "总金额"
  },
  "trade_amount": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "交易金额",
    "label": "交易金额"
  },
  "store_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "店铺id",
    "label": "店铺id"
  },
  "coupon_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "优惠券id",
    "label": "优惠券id"
  }
}

const enumConverter = {
  "pay_status_valuetotext": {
    "1": "未付款",
    "2": "已付款",
    "3": "支付中"
  },
  "pay_type_valuetotext": {
    "alipay": "支付宝",
    "wechat": "微信付款"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
